package com.sen.common.widget.form;

import org.apache.commons.lang.StringUtils;

import com.sen.common.widget.Attribute;
import com.sen.common.widget.Attributes;
import com.sen.common.widget.Component;

/**
 * @author sen
 */
public class SelectOption extends Component {
	private String value;
	private String selected;

	public SelectOption(String value, String text) {
		super("option");
		this.value = value;
		String content = replaceFrontSpaceWithHtmlFormat(text);
		this.attr(new Attribute().attr("title", content)).attr(new Attribute().attr("value", value)).add(
				new Content(content));
	}

	private String replaceFrontSpaceWithHtmlFormat(String text) {
		if (null == text) {
			return StringUtils.EMPTY;
		}
		return text.replaceAll("(?m)(?<=^ *) ", "&nbsp;");
	}

	public SelectOption selected() {
		return selected(true);
	}

	public SelectOption selected(boolean selected) {
		if (selected) {
			this.selected = "selected";
		}
		return this;
	}

	@Override
	protected String specialAttributes() {
		return Attributes.attrIfNonblank("selected", selected);
	}

	public String getValue() {
		return value;
	}
}
